﻿
using EvaluationSystem.Dto;
using EvaluationSystem.Models;
using EvaluationSystem.Models.Views;
using EvaluationSystem.Utils;
using EvaluationSystem.ViewModels;

using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;

namespace EvaluationSystem.Apis
{

    [ApiController]
    public class CommentController : ApiBaseController<CommentController>
    {
        /// <inheritdoc />
        public CommentController(ILogger<CommentController> logger, MyDbContext dbContext) : base(logger, dbContext)
        {
        }

        [HttpGet]
        public IActionResult Test()
        {
            return Content("ok");
        }

        /// <summary>
        /// 获取评论
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Get(int id)
        {
            var comments = await _dbContext.Comment
                .Include(t => t.Publisher)
                .Include(t => t.Replier)
                .Where(t => t.ScId == id)
                .Select(t => new CommentVm(t))
                .ToListAsync();
            return Ok(ResultDto.Ok(data: comments));
        }

        /// <summary>
        /// 添加评论
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Add([FromForm] CommentDto req)
        {
            GetUserToken<People>(out var people);

            Comment comment = new Comment
            {
                PublisherId = people.PeopleId,
                ReplierId = req.rid,
                ScId = req.scid,
                Content = req.Content,
            };
            _dbContext.Comment.Add(comment);
            var num = await _dbContext.SaveChangesAsync();

            return Ok(ResultDto.Ok(data: num));
        }

    }
}
